<?php

class WooAppController{
	private static $base_cmd;
	private static $default_cmd;
	private $controllerMap;
	private $invoked = array();


	function __construct(WooControllerMap $map){
		var_dump($map);
		$this->controllerMap = $map;
		if(! self::$base_cmd ){
			//echo 111;
			self::$base_cmd = new ReflectionClass("WooCommandCommand");
			self::$default_cmd = new WooCommandDefaultCommand();
		}
	}


	function getView(WooControllerRequest $req){
		//var_dump($req);
		$view = $this->getResource($req,"View");
		return $view;
	}

	function getForward(WooControllerRequest $req){
		$forward = $this->getResource($req,"Forward");
		var_dump($forward);
		if($forward){
			$req->setProperty('cmd',$forward);
		}
		return $forward;
	}

	//getResource()方法执行查找操作，用于转向或选择视图
	//会优先查找最具体的命令字符串和状态标记的组合，然后才搜索通用的组合。
	private function getResource(WooControllerRequest $req,$res){
		//得到前一个命令及其执行状态
		$cmd_str = $req->getProperty('cmd');
		/*$previous = $req->getLastCommand();
		var_dump($req,$cmd_str,$previous);
		$status = $previous->getStatus();
		if(!$status){
			$status = 0;
		}*/
		$status = 0;
		$acquire = "get$res";
		
		//$this->controllerMap = $this->controllerMap->viewMap['AddVenue'][0];
		//$this->controllerMap = $this;
		//var_dump('--',$this->controllerMap->viewMap['AddVenue'][0],$acquire,$res);
		//得到前一个命令的资源及其状态
		$resource = $this->controllerMap->$acquire($cmd_str,$status);
		// 查找命令并且状态为0 的资源
		if(! $resource){
			$resource = $this->controllerMap->$acquire('default',$status);
		}
		//其他情况获取'default'失败，状态为0
		if(! $resource){
			$resource = $this->controllerMap->$acquire('default',0);
		}
		return $resource;

	}

	//getCommand()方法负责返回转向中需要使用的所有命令。
	//工作过程是这样：当请求第一次被接受，就会产生一次个cmd属性，本次请求之前没有任何Command执行过
	function getCommand(WooControllerRequest $req){
		$previous = $req->getLastCommand();
		var_dump('aaaa -- '.$previous);
		if(! $previous){
			//这是本次请求调用的第一个命令
			$cmd = $req->getProperty('cmd');
			if(! $cmd){
				var_dump('bbb -- '.$cmd);
				//如果无法得到命令，则使用默认命令
				$req->setProperty('cmd','default');
				return self::$default_cmd;
			}
		}else{
			//之前已经执行过一个命令
			$cmd = $this->getForward($req);
			var_dump('ccc -- '.$cmd);
			if(! $cmd){
				return null;
			}
		}
		//之前已经执行过一个命令
		$cmd = $this->getForward($req);
		if(! $cmd){
			return null;
		}
		//在$cmd变量中保存着命令名称，并将其解析为Command对象
		$cmd_obj = $this->resolveCommand($cmd);
		if(! $cmd_obj){
			throw new WooBaseAppException("couldn't resolve '$cmd' 无法解决 cmd");
		}

		$cmd_class = get_class($cmd_obj);
		$this->invoked[$cmd_class]++;
		if($this->invoked[$cmd_class] > 1){
			throw new WooBaseAppException("circulal forwarding 循环转发");
		}
		//返回Command对象
		return $cmd_obj;
	}


	function resolveCommand($cmd){
		$cmd = str_replace(array('.'.'/'),"",$cmd);
		$classroot = $this->controllerMap->getClassroot($cmd);
		$filepath = "/command/$classroot.php";
		$classname = "WooCommand$classroot";
		if(file_exists($filepath)){
			if(class_exists($classname)){
				$cmd_class = new ReflectionClass($classname);
				if($cmd_class->isSubclassOf(self::$base_cmd)){
					return $cmd_class->newInstance();
				}
			}
		}
		return null;
	}
	
}